﻿/*
 *  XTL - XNA Tool Library, a collection with usefull XNA classes
 * 
 *  Copyright (C) 2010 Sebastian Schäfer
 * 
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XTL.Input
{
    /// <summary>
    /// A simple generic class that holds a current and an old state for comparisons
    /// </summary>
    /// <typeparam name="S">The generic class that is beeing compared</typeparam>
    public class XStateManager<S>
    {
        /// <summary>
        /// The old state object (before).
        /// </summary>
        protected S stateOld;
        /// <summary>
        /// The current state object.
        /// </summary>
        protected S stateCurrent;
        /// <summary>
        /// setting a new state will do the following: old = current, current = value
        /// the getter always returns the current state
        /// </summary>
        public S State { set { SetNewState(value); } get { return stateCurrent; } }

        /// <summary>
        /// custructor, sets the initial state (current &amp; old)
        /// </summary>
        /// <param name="state">initial state</param>
        public XStateManager(S state)
        {
            stateOld = state;
            stateCurrent = state;
        }

        /// <summary>
        /// setting a new state will do the following: old = current, current = new state
        /// </summary>
        /// <param name="state">new state</param>
        public void Update(S state)
        {
            SetNewState(state);
        }

        /// <summary>
        /// setting the new state.
        /// </summary>
        /// <param name="state">The new state object</param>
        protected virtual void SetNewState(S state)
        {
            stateOld = stateCurrent;
            stateCurrent = state;
        }
    }
}
